Kotlin

Interfaces

Swift
                  interface MyInterface {
    fun bar()
    fun foo() {
      // optional body
    }
}
                
                    protocol MyInterface {
    func bar()
}

extension MyInterface {
    func foo() {
        // optional body
    }
}

                  

Implementing Interfaces

                  class Child : MyInterface {
    override fun bar() {
        // body
    }
}
                
                    class Child: MyInterface {
    func bar() {
        // body
    }
}

//or

extension Child: MyInterface {
    func bar() {
        // body
    }
}


                  

Properties in Interfaces

                  interface MyInterface {
    val prop: Int // abstract
​
    val propertyWithImplementation: String
        get() = "foo"
​
    fun foo() {
        print(prop)
    }
}
​
class Child : MyInterface {
    override val prop: Int = 29
}
                
                    protocol MyInterface {
    var prop: Int { get }
    
    var propertyWithImplementation: String { get }
    
    func foo()
}

extension MyInterface {
    var propertyWithImplementation: String {
        get { return "foo" }
    }
}

class Child: MyInterface {
    var prop: Int = 29
    
    func foo() {
        print(prop)
    }
}

                  

Interfaces Inheritance

                  interface Named {
    val name: String
}
​
interface Person : Named {
    val firstName: String
    val lastName: String
    
    override val name: String get() = "$firstName $lastName"
}
​
data class Employee(
    // implementing 'name' is not required
    override val firstName: String,
    override val lastName: String,
    val position: Position
) : Person
                
                    import Foundation

protocol Named {
    var name: String { get }
}

protocol Person: Named {
    var firstName: String { get }
    var lastName: String { get }
}

extension Person {
    var name: String {
        get { "\(firstName) \(lastName)" }
    }
}

class Employee: Person {
    let firstName: String
    let lastName: String
    let position: Position
    
    init(firstName: String, lastName: String) {
        self.firstName = firstName
        self.lastName = lastName
    }
}

                  

Resolving overriding conflicts

                  interface A {
    fun foo() { print("A") }
    fun bar()
}
​
interface B {
    fun foo() { print("B") }
    fun bar() { print("bar") }
}
​
class C : A {
    override fun bar() { print("bar") }
}
​
class D : A, B {
    override fun foo() {
        super<A>.foo()
        super<B>.foo()
    }
​
    override fun bar() {
        super<B>.bar()
    }
}
                
                    protocol A {
    func foo()
    func bar()
}
extension A {
    func foo() { print("A") }
}


protocol B {
    func foo()
    func bar()
}
extension B {
    func foo() { print("B") }
    func bar() { print("bar") }
}


class C: A {
    func bar() { print("bar") }
}

//call default implementation in method is not supportted